﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace SMSPanasonic.Business
{
    public partial class pa_Warranty
    {
    	#region public properties
		public string ModelCode { get; set; }
		public string ProductCode { get; set; }
		#endregion

		#region constructor
		public pa_Warranty(){ }

		public pa_Warranty(IDataReader rd)
		{
			this.ModelCode = rd["ModelCode"].ToString();
			this.ProductCode = rd["ProductCode"].ToString();
		}
		#endregion

		#region static method
        public static bool CheckExists(string modelCode, string productCode)
        {
            using (SqlConnection conn = new SqlConnection(AppConfig.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("usp_pa_Warranty_CheckExists", conn))
                {
                    cmd.CommandTimeout = 30;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ModelCode", modelCode);
                    cmd.Parameters.AddWithValue("@ProductCode", productCode);
                    return (bool)cmd.ExecuteScalar();
                }
            }
        }

        public static DateTime? GetWarrantyExtentRegisterDate(string modelCode, string productCode, DateTime registerDate)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(AppConfig.ConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("usp_GetWarrantyExtentRegisterDate", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 30;
                        cmd.Parameters.AddWithValue("@ModelCode", modelCode);
                        cmd.Parameters.AddWithValue("@ProductCode", productCode);
                        cmd.Parameters.AddWithValue("@RegisterDate", registerDate);
                        return (DateTime)cmd.ExecuteScalar();
                    }
                }
            }
            catch
            {
                return null;
            }
        }

        public static bool Insert(pa_WarrantyWaiting objWaiting, string message)
        {
            try
            {
                pa_WarrantyPackage warrantypackage = pa_WarrantyPackage.GetOne(objWaiting.WarrantyPackageCode);
                SqlParameter[] param = new SqlParameter[]
                    {
                        new SqlParameter("@ModelCode", objWaiting.ModelCode),
                        new SqlParameter("@ProductCode", objWaiting.ProductCode),
                        new SqlParameter("@AgencyID", objWaiting.AgencyID),
                        new SqlParameter("@CustomerPhone",objWaiting.CustomerPhone),
                        new SqlParameter("@WarrantyPackageID", string.IsNullOrEmpty(objWaiting.WarrantyPackageCode)? 
                                                            (object)DBNull.Value: 
                                                            warrantypackage.WarrantyPackageID),
                        new SqlParameter("@CreatedDate", objWaiting.ModifiedDate ?? objWaiting.CreatedDate),
                        new SqlParameter("@RegisterDate",objWaiting.RegisterDate),
                        new SqlParameter("@ConfirmDate", DateTime.Now),
                        new SqlParameter("@ExpiredDate", objWaiting.WarrantyType == eWarrantyType.DK ?
                                                        pa_Model.GetExpiredDate(objWaiting.ModelCode, objWaiting.RegisterDate):
                                                        pa_WarrantyPackage.GetExpriredDate(warrantypackage.WarrantyPackageCode, objWaiting)),
                        new SqlParameter("@UserID", DBNull.Value),
                        new SqlParameter("@WarrantyType", objWaiting.WarrantyType),
                        new SqlParameter("@Description", message),
                        new SqlParameter("@RegisteredFrom",objWaiting.RegisteredFrom),
                        new SqlParameter("@RegisteredPhone", objWaiting.RegisteredPhone ?? (object)DBNull.Value),
                        new SqlParameter("@WarrantyStatusID",pa_WarrantyStatus.GetOne(eWarrantyStatus.XACNHANBAOHANH).WarrantyStatusID)
                    };
                var retvalue = SQLCommand.ExecuteScalar("usp_pa_Warranty_Insert", CommandType.StoredProcedure, param); 
                if (retvalue != null)
                    return (bool)retvalue;
                return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

		#endregion
	}
}
